package cn.kimming.wms.web.action;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionContext;

import cn.kimming.wms.anno.RequiredPermission;
import cn.kimming.wms.domain.OrderGroupType;
import cn.kimming.wms.domain.SaleGroupType;
import cn.kimming.wms.query.OrderChartQueryObject;
import cn.kimming.wms.query.SaleChartQueryObject;
import cn.kimming.wms.service.IBrandService;
import cn.kimming.wms.service.IChartService;
import cn.kimming.wms.service.IClientService;
import cn.kimming.wms.service.ISupplierService;
import cn.kimming.wms.vo.OrderChartVO;
import cn.kimming.wms.vo.SaleChartVO;
import lombok.Getter;

@Controller("chartAction")
@Scope("prototype")
public class ChartAction extends BaseAction {

	private static final long serialVersionUID = 7964496146418099645L;

	@Autowired
	private IChartService chartService;
	@Autowired
	private ISupplierService supplierService;
	@Autowired
	private IClientService clientService;
	@Autowired
	private IBrandService brandService;

	@Getter
	private OrderChartQueryObject oqo = new OrderChartQueryObject();
	@Getter
	private SaleChartQueryObject sqo = new SaleChartQueryObject();

	@RequiredPermission("订货报表")
	public String orderChart() throws Exception {
		try {
			ActionContext.getContext().put("objs", chartService.queryOrderChart(oqo));
			ActionContext.getContext().put("suppliers", supplierService.list());
			ActionContext.getContext().put("brands", brandService.list());
			ActionContext.getContext().put("groupTypes", OrderGroupType.values());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "orderChart";
	}

	@RequiredPermission("销售报表")
	public String saleChart() throws Exception {
		try {
			ActionContext.getContext().put("objs", chartService.querySaleChart(sqo));
			ActionContext.getContext().put("clients", clientService.list());
			ActionContext.getContext().put("brands", brandService.list());
			ActionContext.getContext().put("groupTypes", SaleGroupType.values());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "saleChart";
	}

	@RequiredPermission("销售报表线性图")
	public String saleChartByLine() throws Exception {
		try {
			ActionContext.getContext().put("groupType", SaleGroupType.valueOf(sqo.getSaleGroupType()).getGroupType());
			List<SaleChartVO> vos = chartService.querySaleChart(sqo);
			List<String> groupTypes = new ArrayList<>();
			List<BigDecimal> amounts = new ArrayList<>();
			for (SaleChartVO vo : vos) {
				groupTypes.add(vo.getGroupType());
				amounts.add(vo.getTotalAmount());
			}
			ActionContext.getContext().put("groupTypes", JSON.toJSONString(groupTypes));
			ActionContext.getContext().put("amounts", JSON.toJSONString(amounts));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "saleChartByLine";
	}

	@RequiredPermission("订货报表线性图")
	public String orderChartByLine() throws Exception {
		try {
			ActionContext.getContext().put("groupType", OrderGroupType.valueOf(oqo.getOrderGroupType()).getGroupType());
			List<OrderChartVO> vos = chartService.queryOrderChart(oqo);
			List<String> groupTypes = new ArrayList<>();
			List<BigDecimal> amounts = new ArrayList<>();
			for (OrderChartVO vo : vos) {
				groupTypes.add(vo.getGroupType());
				amounts.add(vo.getTotalAmount());
			}
			ActionContext.getContext().put("groupTypes", JSON.toJSONString(groupTypes));
			ActionContext.getContext().put("amounts", JSON.toJSONString(amounts));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "orderChartByLine";
	}

	@RequiredPermission("销售报表扇形图")
	public String saleChartByPie() throws Exception {
		try {
			ActionContext.getContext().put("groupType", SaleGroupType.valueOf(sqo.getSaleGroupType()).getGroupType());
			List<SaleChartVO> vos = chartService.querySaleChart(sqo);
			List<Object[]> datas = new ArrayList<>();
			for (SaleChartVO vo : vos) {
				datas.add(new Object[] { vo.getGroupType(), vo.getTotalAmount() });

			}
			ActionContext.getContext().put("datas", JSON.toJSONString(datas));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "saleChartByPie";
	}
	@RequiredPermission("销售报表扇形图")
	public String orderChartByPie() throws Exception {
		try {
			ActionContext.getContext().put("groupType", OrderGroupType.valueOf(oqo.getOrderGroupType()).getGroupType());
			List<OrderChartVO> vos = chartService.queryOrderChart(oqo);
			List<Object[]> datas = new ArrayList<>();
			for (OrderChartVO vo : vos) {
				datas.add(new Object[] { vo.getGroupType(), vo.getTotalAmount() });
				
			}
			ActionContext.getContext().put("datas", JSON.toJSONString(datas));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "orderChartByPie";
	}
}
